package cn.hss.map;

/**
 * @author 作者：HeShaoShuai
 * @Email 邮箱：2569819541@qq.com
 * @Date 日期：2023/4/28 8:08
 * @Package 包名：cn.hss.map
 * @Project 项目名：JavaSE
 * @Version 1.0
 */
public class Person implements Comparable {
	private String name;
	private int age;

	public Person() {
	}

	/*重构compareTo()方法的实现逻辑
	需求：先按照年龄从小到大的顺序排列，即升序；
	如果年龄相等，则按照name从大到小的顺序排序，即降序
	* */
	@Override
	public int compareTo(Object o) {
		if (this == o) {//两个比较的对象相等，返回0
			return 0;
		}
		if (o instanceof Person) {
			Person person = (Person) o; //向下转型
			//用当前的年龄和传递过来的参数的年龄进行比较
			int compare = this.age - person.age;
			if (compare != 0) {
				return compare;
			} else {
				//如果年龄相等，则按照name从大到小的顺序排序，即降序
				return -this.name.compareTo(person.getName());
			}
		} else {
			throw new RuntimeException("参数类型不匹配");
		}
	}

	@Override
	public String toString() {
		return "Person{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(final int age) {
		this.age = age;
	}
}